package main.java.pers.seg.model.utils;

import main.java.pers.seg.model.constants.GlobalConstants;
import main.java.pers.seg.model.model.Activity;
import main.java.pers.seg.model.model.View;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;



import com.sun.org.apache.xalan.internal.xsltc.dom.DocumentCache;

import static main.java.pers.seg.model.constants.GlobalConstants.activityList;

/**
 * Created by 金珉志
 * 2018/3/4.
 */
public class FileUtils {
    /*public static void createXMLByDOM(String fileName) {
        File file=new File(GlobalConstants.GEN_XML_HOME+".xml");
        // 创建DocumentBuilderFactory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            // 创建DocumentBuilder
            DocumentBuilder builder = factory.newDocumentBuilder();

            // 创建Document
            Document document = builder.newDocument();

            // 设置XML声明中standalone为yes，即没有dtd和schema作为该XML的说明文档，且不显示该属性
            // document.setXmlStandalone(true);

            // 创建根节点
            Element row = document.createElement("row");

            // 创建子节点，并设置属性
            Element col = document.createElement("col");
            col.setAttribute("id", "1");
            col.setAttribute("isauth","1");
            col.setAttribute("title","授权的接口");
            // 为book添加子节点
            Element functions = document.createElement("functions");
            col.appendChild(functions);
            for(int i=0;i<list.size();i++){
                Element function = document.createElement("function");
                function.setAttribute("id",list.get(i).getVcInterfaceNumber());
                function.setAttribute("isproperties","0");
                function.setAttribute("servicename","");
                functions.appendChild(function);
            }

            // 为根节点添加子节点
            row.appendChild(col);

            // 将根节点添加到Document下
            document.appendChild(row);

            *//*
             * 下面开始实现： 生成XML文件
             *//*

            // 创建TransformerFactory对象
            TransformerFactory tff = TransformerFactory.newInstance();

            // 创建Transformer对象
            Transformer tf = tff.newTransformer();

            // 设置输出数据时换行
            tf.setOutputProperty(OutputKeys.INDENT, "yes");

            // 使用Transformer的transform()方法将DOM树转换成XML
            tf.transform(new DOMSource(document), new StreamResult(file));

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }*/

    public static void ReadTreeStructure() {

        SAXReader reader = new SAXReader();
        //读取文件 转换成Document
        Document document = null;
        try {
            document = (Document) reader.read(new File(GlobalConstants.GEN_XML_HOME));
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        //获取根节点元素对象
        Element root = document.getRootElement();

        //遍历
        listNodes(root);

        for(int i=0;i<activityList.size();i++){
            System.out.println(activityList.get(i));
        }
    }

    public static void listNodes(Element node){
        boolean flag = false;
        if(!node.getName().equals("GUIHierarchy")) {
            System.out.println("当前节点的名称：" + node.getName());
        }
        //首先获取当前节点的所有属性节点
        List<Attribute> list = node.attributes();

        Activity activity = new Activity();
        View view = new View();
        List<View> viewList = null;
        //遍历属性节点
        if(!node.getName().equals("GUIHierarchy")) {
            for (Attribute attribute : list) {

                if(node.getName().equals("Activity")) {
                    activity.setName(attribute.getValue());
                }
                else if(node.getName().equals("View")){
                    if(attribute.getName().equals("type")){
                        view.setType(attribute.getValue());
                    }
                    else if(attribute.getName().equals("id")) {
                        view.setId(attribute.getValue());
                    }
                    else if(attribute.getName().equals("idName")) {
                        view.setIdName(attribute.getValue());
                    }
                }

                System.out.println("属性" + attribute.getName() + ":" + attribute.getValue());

            }
            if(node.getName().equals("Activity")){
                System.out.println("&&&"+ activity.getName());
            }
            if(node.getName().equals("View")){
                System.out.println("***"+ view.getType());
                viewList.add(view);
                activity.setViewList(viewList);
                //activityList.add(activity);
            }
        }
        //如果当前节点内容不为空，则输出
        if(!(node.getTextTrim().equals(""))){
            System.out.println( node.getName() + "：" + node.getText());
        }
        //同时迭代当前节点下面的所有子节点
        //使用递归
        Iterator<Element> iterator = node.elementIterator();
        while(iterator.hasNext()){
            Element e = iterator.next();
            listNodes(e);
        }

        //activityList.add(activity);
    }



}
